package cn.heygod.airsql.executor.jdbc;

import cn.heygod.airsql.executor.SqlExecutor;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @className: JDBCTemplateExecutor
 * @author: Simon
 * @description: TODO
 * @date: 2024/1/26 21:36
 * @version: 1.0
 */
public class JDBCTemplateExecutor implements SqlExecutor {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int update(String sql, List params){
        return jdbcTemplate.update(sql, params.toArray());
    }

    public int delete(String sql, List params){
        return jdbcTemplate.update(sql, params.toArray());
    }

    public <T> List<T> query(Class<T> modelClass,String sql, List params){
        JdbcRowMapper mapper = new JdbcRowMapper(modelClass);
        return jdbcTemplate.query(sql,new JdbcRowMapper<>(modelClass),params.toArray() );
    }

    public <T> T queryOne(Class<T> modelClass,String sql, List params){
        JdbcRowMapper<T> mapper = new JdbcRowMapper(modelClass);
        return jdbcTemplate.queryForObject(sql,mapper,params.toArray() );
    }

    public Integer queryInt(String sql, List params){
        return jdbcTemplate.queryForObject(sql, Integer.class,params.toArray());
    }
}
